package com.yangtao.framework.web.struts2;

import com.yangtao.framework.common.Message;
import com.yangtao.framework.common.Pagination;
import com.yangtao.framework.common.SystemConstant;
import com.yangtao.framework.web.query.QueryCondition;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseModel implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 系统使用的sessionId
	 */
	private String sessionId;
	/**
	 * 用户Id
	 */
	private String userId;
	/**
	 * 用户名称
	 */
	private String userName;
	/**
	 * 用户类型
	 */
	private String userType;
	/**
	 * 公司Id
	 */
	private String companyId;
	/**
	 * 部门Id
	 */
	private String organizationId;
	/**
	 * 处理区分
	 */
	private String processType;

	/**
	 * 消息
	 */
	private List<Message> message = null;
	/**
	 * 封装分页对象
	 */
	protected Pagination page = new Pagination();

	/**
	 * 查询条件
	 */
	protected List<QueryCondition> condition;


	public List<QueryCondition> getCondition() {
		return condition;
	}

	public void setCondition(List<QueryCondition> condition) {
		this.condition = condition;
	}

	/**
	 * 判定是否有错误
	 *
	 * @return
	 */
	public boolean hasError() {
		if (message == null)
			return false;
		for (Message m : message) {
			if (m.getType() == SystemConstant.ERROR)
				return true;
		}
		return false;
	}

	/**
	 * 增加一个错误消息
	 *
	 * @param srcId 错误元素的ID
	 * @param key   错误消息的key
	 * @param param 错误消息的参数
	 */
	public void addError(String srcId, String key, String[] param) {
		if (this.message == null)
			this.message = new ArrayList<Message>();
		Message message = new Message(srcId, key, param, SystemConstant.ERROR);
		this.message.add(message);
	}

	/**
	 * 增加一个错误消息
	 *
	 * @param key   错误消息的key
	 * @param param 错误消息的参数
	 */
	public void addError(String key, String[] param) {
		addError(null, key, param);
	}

	public void addError(String key) {
		addError(null, key, null);
	}

	public void addWarning(String srcId, String key, String[] param) {
		if (this.message == null)
			this.message = new ArrayList<Message>();
		Message message = new Message(srcId, key, param, SystemConstant.WARNNING);
		this.message.add(message);
	}

	public void addWarning(String key, String[] param) {
		addWarning(null, key, param);
	}

	public void addWarning(String key) {
		addWarning(null, key, null);
	}

	public void addInfo(String key, String[] param) {
		if (this.message == null) {
			this.message = new ArrayList<Message>();
		}
		Message message = new Message(key, param);
		this.message.add(message);
	}

	public void addInfo(String key) {
		addInfo(key, null);
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(String organizationId) {
		this.organizationId = organizationId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public String getProcessType() {
		return processType;
	}

	public void setProcessType(String processType) {
		this.processType = processType;
	}

	public List<Message> getMessage() {
		return message;
	}

	public void setMessage(List<Message> message) {
		this.message = message;
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}
}
